package com.fy.exam.mapper;

import com.fy.exam.entity.Exam;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 Mapper 接口
 * </p>
 *
 * @author 
 * @since 2023-06-14
 */
public interface ExamMapper extends BaseMapper<Exam> {

    @Select("-- 完全公开的考试\n" +
            "SELECT el_exam.* FROM el_exam WHERE open_type =1\n" +
            "UNION\n" +
            "-- 指定部门，用户在当前部门\n" +
            "SELECT el_exam.* FROM el_exam LEFT JOIN el_exam_depart\n" +
            "ON el_exam.id = el_exam_depart.exam_id\n" +
            "WHERE el_exam_depart.depart_id=(\n" +
            "\tSELECT dept_id FROM sys_user WHERE user_id=#{userId}\n" +
            ")\n" +
            "UNION\n" +
            "-- 指定用户\n" +
            "SELECT el_exam.* FROM el_exam LEFT JOIN el_exam_user\n" +
            "ON el_exam.id = el_exam_user.exam_id\n" +
            "WHERE el_exam_user.user_id = #{userId}")
    public List<Exam> findMyExam(String userId);


    //统计主观题的总分
    @Select("SELECT SUM(jdt_count*jdt_score) AS sub_score\n" +
            "FROM el_exam_repo\n" +
            "WHERE exam_id = #{examId}")
    public Map totalSubScore(String examId);

}
